package com.gdkj.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.gdkj.util.AwdbDb.AWReader;
import lombok.extern.slf4j.Slf4j;
import main.java.com.gdkj.util.AwdbAddressVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;

@Slf4j
public class AwdbUtils {
    public static JsonNode getIpAddressInfo(String ip, String filePath) throws IOException {
        AWReader awReader = new AWReader(new File(filePath));
        log.info("本次请求IP：" + ip);
        log.info("文件地址：" + filePath);
        InetAddress address = InetAddress.getByName(ip);
        return awReader.get(address);
    }

    public static AwdbAddressVO getIpAddress(String ip, String filePath) throws IOException {
        AWReader awReader = new AWReader(new File(filePath));
        log.info("本次请求IP：" + ip);
        log.info("文件地址：" + filePath);
        InetAddress address = InetAddress.getByName(ip);
        JsonNode jsonNode = awReader.get(address);
        AwdbAddressVO vo = JSONObject.parseObject(jsonNode.toString(),AwdbAddressVO.class);
        return vo;
    }

    public static AwdbAddressVO getAddressByIp(String ip){
        try {
            return getIpAddress(ip,"/data/ip/IP_basic_single_WGS84.awdb");
        }catch (Exception e){
            log.error("获取地址失败");
        }
        return null;
    }
}
